package com.nullpointer.xchat.fragment;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.nullpointer.xchat.R;

public class LicenseDialogFragment extends DialogFragment {
	private TextView tvContent;
	private Button btCancel;
	private Button btAccept;
	private OnLicenseDialogFragment listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_license, container);
		findViewIds(view);
		init();
		setEvent();
		return view;
	}

	private void init() {
		getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
		tvContent.setMovementMethod(new ScrollingMovementMethod());
		String content = readFile();
		tvContent.setText(content);
	}

	private void setEvent() {
		btAccept.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				if (listener != null) {
					listener.onAccept();
				}
			}
		});

		btCancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
				if (listener != null) {
					listener.onCancel();
				}
			}
		});
	}

	private void findViewIds(View view) {
		btAccept = (Button) view.findViewById(R.id.bt_accept);
		btCancel = (Button) view.findViewById(R.id.bt_cancel);
		tvContent = (TextView) view.findViewById( R.id.tv_content );
	}

	public String readFile(){
		BufferedReader reader = null;
		StringBuilder returnString = new StringBuilder();
		try {
		    reader = new BufferedReader(
		        new InputStreamReader(getActivity().getAssets().open("policy.txt"), "UTF-8")); 

		    // do reading, usually loop until end of file reading 
		    String mLine = "";
		    while ((mLine = reader.readLine()) != null) {
		    	returnString.append("\n" + mLine);
		    }
		} catch (IOException e) {
		    //log the exception
		} finally {
		    if (reader != null) {
		         try {
		             reader.close();
		         } catch (IOException e) {
		             //log the exception
		         }
		    }
		}
		return returnString.toString();
	}

	@Override
	public void onDismiss(DialogInterface dialog) {
		super.onDismiss(dialog);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		super.onCancel(dialog);
	}

	public void setOnExitDialogListener(OnLicenseDialogFragment listener) {
		this.listener = listener;
	}

	public interface OnLicenseDialogFragment {
		public void onCancel();

		public void onAccept();
	}
}
